/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinotrack.domain;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

/**
 *
 * @author alquad
 */
public class PersonaSQL {

    Session session = null;

    public PersonaSQL() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public boolean registrarPersona(Persona persona) {
        try {
            session.beginTransaction();
            SQLQuery q = session.createSQLQuery("call persona_registrar(?, ?, ?, ?)");
            q.setParameter(0, persona.getUsuario().getUsuario());
            q.setParameter(1, persona.getNombre());
            q.setParameter(2, persona.getApellido());
            q.setParameter(3, persona.getEmail());
            q.executeUpdate();
            session.getTransaction().commit();

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public Persona getPersona(String nombreUsuario) {
        try {
            session.beginTransaction();
            Query q = session.createQuery("from Persona where usuario.usuario like '"+nombreUsuario+"'");
            Persona persona = (Persona) q.list().get(0);

            return persona;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
